<?php
namespace app\index\controller;

use app\common\model\Article as ArticleModel;
use app\common\model\Comment as CommentModel;
use app\common\model\Category as CategoryModel;
use app\common\model\Tag as TagModel;
use think\Controller;
use think\Session;

class Index extends Controller
{
    public function _initialize()
    {
        parent::_initialize();
        $yearMonth = ArticleModel::field('FROM_UNIXTIME(unix_timestamp(created_at), \'%Y年%m月\') as ym')
            ->group('ym')
            ->order('ym desc')
            ->select()
        ;
        $ym_data = [];
        foreach ($yearMonth as $v) {
            $month = $v['ym'];
            $ym_data[$month] = ArticleModel::field('id,title')
                ->where("FROM_UNIXTIME(unix_timestamp(created_at), '%Y年%m月') = '$month'")
                ->select();
        }

        $article_list = ArticleModel::field('id,title')
            ->where(['status'=>'publish'])
            ->order('updated_at', 'desc')
            ->limit(0, 10)
            ->select()
        ;
        $comments = CommentModel::where(['pid'=>0])
            ->with([
                'user.image',
                'article'=>function($query){$query->withField('id,title');}])
            ->limit(0, 10)
            ->order('created_at', 'asc')
            ->select();
        $categories = CategoryModel::getTreeData('level', 'sort_number');
        $tags = TagModel::all();
        $this->assign(compact('article_list', 'comments', 'categories', 'tags', 'ym_data'));
    }

    public function index()
    {
        return view();
    }

    public function blog($uid = 0, $cid = 0, $tid = 0)
    {
        $search = input('get.search');
        if ($uid < 0 || $cid < 0 || $tid < 0) return $this->error('非法访问');
        if ($tid == 0 && $cid > 0)
            $articles = ArticleModel::hasWhere('categories', ['category_id'=>['in', implode(',', CategoryModel::getParentId($cid)) . ',' . $cid]]);
        else if ($tid == 0 && $cid == 0)
            $articles = ArticleModel::hasWhere('categories');
        else if ($tid > 0 && $cid == 0)
            $articles = ArticleModel::hasWhere('tags', ['tag_id'=>$tid]);
        else
            return $this->error('无法找到文件');

        if ($uid > 0) $articles = $articles->where(['user_id'=>$uid]);

        $articles = $articles
            ->order('updated_at desc')
            ->with([
                'user',
                'categories.category',
                'comments',
                'image'
            ])
            ->where(['status'=>'publish', 'title'=>['like','%' . trim($search) . '%']])
            ->paginate(5)
        ;
        $page = $articles->render();
        $this->assign(compact('articles', 'page'));
        return view();
    }

    public function article($id)
    {
        $article = ArticleModel::where(['status'=>'publish', 'id'=>$id])
            ->with(['user', 'categories.category', 'tags.tag', 'image'])
            ->find()
        ;
        $login = Session::get('login');
        $comment_count = CommentModel::where(['article_id'=>$id])->count();

        //dump($article);
        $this->assign(compact('article', 'login', 'comment_count'));
        return view();
    }

    public function comment($id)
    {
        $comments = CommentModel::getTreeData($id, 'level', 'created_at');
        $this->assign(compact('comments'));
        return view();
    }



}
